﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace DPMQWindowsService
{
    public partial class DPMQServce : ServiceBase
    {


        System.Timers.Timer timerMQ;  //计时器

               
        public DPMQServce()
        {
            InitializeComponent();
        }



        protected override void OnStart(string[] args)
        {
            try
            {
                MQRPServer.Subscribe =false ;                
                if (timerMQ==null)
                {
                    timerMQ = new System.Timers.Timer();  //计时器
                }              
                timerMQ.Interval = 3000;            //设置计时器事件间隔执行时间
                timerMQ.Elapsed += new System.Timers.ElapsedEventHandler(ChkService);       //到达时间执行的事件
                //  timer1.AutoReset = true;                                             
                //设置执行一次（false）还是一直执行（true）
                timerMQ.Enabled = true;
            }
            catch (Exception ex)
            {
                LogWriter.Instance.writeError(ex.ToString());
            }
        }



        /// <summary>
        /// 停止服务
        /// </summary>
        protected override void OnStop()
        {

            try
            {              
                timerMQ.Elapsed -= new System.Timers.ElapsedEventHandler(ChkService);                 
                timerMQ.Enabled = false;
                MQRPServer.Subscribe = false;
            }
            catch (Exception ex)
            {
                LogWriter.Instance.writeError(ex.ToString());
            }
        }






        /// <summary>
        /// 定时检查，并执行方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        public void ChkService(object source, System.Timers.ElapsedEventArgs e)
        {

            try
            {
                if (MQRPServer.Subscribe != true)
                {
                    MQRPServer.Consumer();
                }
            }
            catch (Exception err)
            {
                LogWriter.Instance.writeError(err.ToString());
            }

        }














    }
}
